﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using DataAccess;
namespace QuanLyDiemSV
{
    public partial class DangNhap : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
           
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            SqlConnection cn = ConnectDB.Getconnect();
            cn.Open();
            SqlCommand cmd = new SqlCommand("Login_Account", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@UserName", SqlDbType.NVarChar).Value = TextBox1.Text;
            cmd.Parameters.Add("@PassWord", SqlDbType.NVarChar).Value = TextBox2.Text;
            ConnectDB.rd = cmd.ExecuteReader();
            bool login = ConnectDB.rd.Read();
            if (login)
            {
                int check = phanquyen(TextBox1.Text);
                switch (check)
                {
                    case 1:  
                        Session["dangnhapadmin"] = "dangnhapadmin";
                        Response.Write("<script>alert('ban đăng nhập thành công');</script");
                        Response.Redirect("HomePageAdmin.aspx");
                        break;
                    case 2:
                        Response.Write("<script>alert('ban đăng nhập thành công');</script");
                        Session["dangnhap"] = "dangnhap";
                        Response.Redirect("HomePage.aspx");
                        break;
                }
            }
        }
        protected int phanquyen(string input)
        {
            SqlConnection cn = ConnectDB.Getconnect();
            cn.Open();
            SqlCommand cmd = new SqlCommand("PhanQuyen", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@UserName", SqlDbType.NVarChar).Value = input;
            int maquyen = (int)cmd.ExecuteScalar();
            return maquyen;
        }

       
    }
}